package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.model.param.JoinOrderParam;
import com.yungu.swift.order.model.vo.JsonOrderDetailVo;
import com.yungu.swift.order.service.OrderFareService;
import com.yungu.swift.order.service.OrderJoinService;
import com.yungu.swift.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v2/passenger/")
@Api(value = "【乘客】【订单】【跨城小件】乘客跨城小件订单接口", tags = "乘客订单相关操作")
public class ParcelOrderApi extends BaseApi {

    @Reference
    private OrderService orderService;
    @Reference
    private OrderJoinService orderJoinService;
    @Reference
    private OrderFareService orderFareService;

    @RequestMapping(value = {"/token/join/order/parcel/evaluate"})
    @ApiOperation(value = "估价接口 —— 已生效", response = Map.class, notes = "响应数据结构参照专/快车系列，以实际接口返回为准")
    public ResponseData<Map<String, Object>> parcelEvaluate(HttpServletRequest request,
                                                            @RequestHeader String appid,
                                                            @RequestBody JoinOrderParam joinOrdeParam) {
        joinOrdeParam.setPassengerUuid(getUserUid(request));
        joinOrdeParam.setAppid(appid);
        return orderFareService.parcelValuation(joinOrdeParam);
    }

    @RequestMapping(value = {"/token/join/order/parcel/create"})
    @ApiOperation(value = "创建订单接口 —— 已生效", response = JsonOrderDetailVo.class)
    public ResponseData<JsonOrderDetailVo> createParcelOrder(HttpServletRequest request, @RequestHeader String appid,
                                                             @RequestBody JoinOrderParam joinOrdeParam) {
        joinOrdeParam.setPassengerUuid(getUserUid(request));
        joinOrdeParam.setAppid(appid);
        joinOrdeParam.setTypeModule(CommonConstant.BUSINESS_TYPE_PARCEL);
        //服务端临时兼容处理
        if (joinOrdeParam.getDeparTime() == null && joinOrdeParam.getDeparTime() != null) {
            joinOrdeParam.setDeparTime(joinOrdeParam.getDeparTime());
        }
        ResponseData<Map<String, Object>> crosstownOrder = orderJoinService.createCrosstownOrder(joinOrdeParam);
        if (!crosstownOrder.isSuccess() || crosstownOrder.getData() == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, crosstownOrder.getMsg());
        }
        String orderUuid = crosstownOrder.getData().get("orderUuid").toString();
        return orderService.getOrderDetail(orderUuid, null);
    }

    @RequestMapping(value = {"/token/join/order/parcel/tag"})
    @ApiOperation(value = "跨城小件标签获取接口", response = ResponseData.class)
    public ResponseData<Map<String, Object>> getParcelTag(@RequestHeader String appid, @RequestBody JoinOrderParam joinOrdeParam) {
        joinOrdeParam.setAppid(appid);
        return orderJoinService.getParcelTag(joinOrdeParam);
    }

    @RequestMapping(value = {"/token/join/order/parcel/getReceiptCode"})
    @ApiOperation(value = "乘客获取收件码接口", response = ResponseData.class)
    public ResponseData<List<Map<String, Object>>> getReceiptCode(@RequestHeader String appid, @RequestBody JoinOrderParam joinOrdeParam) {
        joinOrdeParam.setAppid(appid);
        return orderJoinService.getReceiptCode(joinOrdeParam);
    }

}
